﻿#region

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Vin.Extension.Cache;
using Vin.Extension.Redis.Cache;
using Vin.Extension.Redis.Models;
using Vin.Tool.AspNetCore.Singleton;

#endregion

namespace Vin.Extension.Redis;

public class ExtensionEntry : IVinExtension
{
    public void AddVinExtension(WebApplicationBuilder builder)
    {

    }

    public void AddVinExtension(IServiceCollection services)
    {
        services.Configure<RedisOption>(VinApp.Configuration.GetSection(VinRedisExtension.OptionKey));
    }

    public void UseVinExtension(WebApplication app)
    {
        var redisOptions = VinApp.Configuration.GetSection(VinRedisExtension.OptionKey).Get<RedisOption>();
        if (redisOptions is { DefaultCache: true })
        {
            var defaultCache = RedisCacheManager.GetRedisCache(redisOptions.DefaultDb);
            CacheFactory.ChangeCacheObj(defaultCache);
        }
    }
}